# ESP32 代码：uart_echo.py
from machine import UART
import time
from load_config import load_dict_from_file, save_dict_to_file


def reload_wifi(received_data):
    if "R_WIFI" in received_data:
        wifi_setting = load_dict_from_file('config.json')
        ret = f"SSID: {wifi_setting['SSID']}, PWD: {wifi_setting['PWD']}"
    elif "W_WIFI_SSID" in received_data:
        ssid = received_data.split(' ')[1].strip()
        wifi_setting = load_dict_from_file('config.json')
        wifi_setting['SSID'] = ssid
        save_dict_to_file(wifi_setting)
        ret = "SSID set successful !"
    elif "W_WIFI_PWD" in received_data:
        pwd = received_data.split(' ')[1].strip()
        wifi_setting = load_dict_from_file('config.json')
        wifi_setting['PWD'] = pwd
        save_dict_to_file(wifi_setting)
        ret = "PWD set successful !"
    else:
        pass
    return ret+ '\n'
    
    

def uart1_handler(received):
    """处理串口信息 """
    ret = reload_wifi(received)    
    return ret
            
